home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
haeberli
/
libgutil
/
rct.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
4KB
|
253 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* rct -
* Management of rectangles.
*
* rectangles are half open. that is if xmin is 0 and xmax is 1,
* the width of the rectangle is 1. You can think of the min and
* may values representing the locations of the edges.
*
* Paul Haeberli - 1986
*/
#include "rct.h"
#include "port.h"
rct *rctnew()
{
rct *r;
r = (rct *)mymalloc(sizeof(rct));
r->xmin = 0;
r->ymin = 0;
r->xmax = 0;
r->ymax = 0;
return r;
}
rctfree(r)
rct *r;
{
free(r);
}
rct *rctclone(r)
rct *r;
{
rct *c;
c = rctnew();
*c = *r;
return c;
}
rctcopy(scr,dst)
rct *scr, *dst;
{
*dst = *scr;
}
rctset(r,x1,y1,x2,y2)
rct *r;
int x1, y1, x2, y2;
{
r->xmin = x1;
r->ymin = y1;
r->xmax = x2;
r->ymax = y2;
rctnormal(r);
}
rctsize(r,ox,oy,sizex,sizey)
rct *r;
int ox, oy;
int sizex, sizey;
{
r->xmin = ox;
r->ymin = oy;
r->xmax = ox+sizex;
r->ymax = oy+sizey;
rctnormal(r);
}
rctnormal(r)
rct *r;
{
int temp;
if(r->xmin > r->xmax) {
temp = r->xmin;
r->xmin = r->xmax;
r->xmax = temp;
}
if(r->ymin > r->ymax) {
temp = r->ymin;
r->ymin = r->ymax;
r->ymax = temp;
}
}
rctoffset(r,dx,dy)
rct *r;
int dx, dy;
{
r->xmin += dx;
r->ymin += dy;
r->xmax += dx;
r->ymax += dy;
}
rctshrink(r,dx,dy)
rct *r;
int dx, dy;
{
r->xmin += dx;
r->ymin += dy;
r->xmax -= dx;
r->ymax -= dy;
rctnormal(r);
}
int rctarea(r)
rct *r;
{
return (r->xmax-r->xmin)*(r->ymax-r->ymin);
}
int rctinter(src1,src2,dest)
rct *src1, *src2, *dest;
{
int xmin, xmax;
int ymin, ymax;
xmin = MAX(src1->xmin,src2->xmin);
xmax = MIN(src1->xmax,src2->xmax);
ymin = MAX(src1->ymin,src2->ymin);
ymax = MIN(src1->ymax,src2->ymax);
if(xmax>xmin && ymax>ymin) {
dest->xmin = xmin;
dest->xmax = xmax;
dest->ymin = ymin;
dest->ymax = ymax;
return 1;
} else {
dest->xmin = 0;
dest->xmax = 0;
dest->ymin = 0;
dest->ymax = 0;
return 0;
}
}
rctunion(src1,src2,dest)
rct *src1, *src2, *dest;
{
int xmin, xmax;
int ymin, ymax;
xmin = MIN(src1->xmin,src2->xmin);
xmax = MAX(src1->xmax,src2->xmax);
ymin = MIN(src1->ymin,src2->ymin);
ymax = MAX(src1->ymax,src2->ymax);
dest->xmin = xmin;
dest->xmax = xmax;
dest->ymin = ymin;
dest->ymax = ymax;
}
/*
* return 1 if r1 is entirely inside r2
*
*/
int rctinrct(r1,r2)
rct *r1, *r2;
{
rct intr;
rctinter(r1,r2,&intr);
if(rctarea(&intr) == rctarea(r1))
return 1;
else
return 0;
}
int rctinside(r,x,y)
rct *r;
int x, y;
{
if ( x >= r->xmin && x < r->xmax &&
y >= r->ymin && y < r->ymax)
return 1;
else
return 0;
}
int rctfinside(r,x,y)
rct *r;
float x, y;
{
if ( x >= r->xmin && x < r->xmax &&
y >= r->ymin && y < r->ymax)
return 1;
else
return 0;
}
int rctequal(r1,r2)
rct *r1, *r2;
{
if ( r1->xmin == r2->xmin && r1->xmax == r2->xmax &&
r1->ymin == r2->ymin && r1->ymax == r2->ymax )
return 1;
else
return 0;
}
int rctempty(r)
rct *r;
{
if(r->xmin == r->xmax || r->ymin == r->ymax)
return 1;
else
return 0;
}
rctcenter(r,xcent,ycent)
rct *r;
int *xcent, *ycent;
{
*xcent = (r->xmin+r->xmax)/2;
*ycent = (r->ymin+r->ymax)/2;
}
rctprint(r)
rct *r;
{
fprintf(stderr,"xy min %d %d xy max: %d %d\n",
r->xmin,r->ymin,r->xmax,r->ymax);
}
rctscale(r,scale)
rct *r;
float scale;
{
r->xmin = r->xmin*scale;
r->ymin = r->ymin*scale;
r->xmax = r->xmax*scale;
r->ymax = r->ymax*scale;
}